package com.sky.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.user.SetmealDishMapper;
import com.sky.mapper.user.UserSetmealMapper;
import com.sky.service.user.UserSetmealService;
import com.sky.vo.DishItemVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserSetmealServiceImpl extends ServiceImpl<UserSetmealMapper, Setmeal> implements UserSetmealService {
    private final UserSetmealMapper userSetmealMapper;
    private final SetmealDishMapper setmealDishMapper;

    @Override
    @Cacheable(value = "setmeal", key = "'list:' + #categoryId", condition = "#categoryId != null" )
    public List<Setmeal> findById(Integer categoryId) {
        if (categoryId == null){
            throw new SetmealEnableFailedException("请选择分类");
        }
        return userSetmealMapper.selectList(new QueryWrapper<Setmeal>()
                .eq("category_id", categoryId)
                .eq("status", 1));
    }

    @Override
    @Cacheable(value = "dish_by_setmeal_id", key = "'list:' + #id", condition = "#id != null")
    public List<DishItemVO> findBySetmealId(Integer id) {
        if (id == null){
            throw new SetmealEnableFailedException("请选择套餐");
        }
        return setmealDishMapper.selectList(new QueryWrapper<SetmealDish>().eq("setmeal_id", id)).stream()
                .map(dish -> {
                    DishItemVO dishItemVO = new DishItemVO();
                    BeanUtils.copyProperties(dish, dishItemVO);
                    return dishItemVO;
                }).toList();
    }
}
